// ==UserScript==
// @name         A营帖子优化
// @version      0.2
// @source       https://gitee.com/Zeng_YL/scratch_addon/tree/master/A营帖子优化
// @updateURL    https://gitee.com/Zeng_YL/scratch_addon/raw/master/A营帖子优化/userscript.user.js
// @description  阿尔法营论坛帖子可：生成目录、网页标题加上帖子标题。目录不支持hash锚点，因为不喜欢。
// @note         参考了A营/Studios/24622/Forum/PostView?postId=7655的效果，但代码完全原创。
// @author       zengyl
// @include      /.*?(aerfaying.com|gitblock.cn).*?/
// @run-at       document-body
// @grant        GM_addStyle
// ==/UserScript==

(function() {

	//主题。note、block
	$("html").attr("BAP-theme", "note");

	//添加css
	GM_addStyle(`
/* 正文的父节点 */
.container>.panel-large .panel2_panelContent_2esaW.panel-content {
	display: flex;
}
/* 正文内容 */
.panel2_panel_1hPqt .forum-post-view_wrapper_s5oGn {
	flex: 5;
}
/* 目录 */
#BAP-Wrapper {
	flex: 1;
	position: sticky;
	top: 70px;
	height: min-content;
	background-color: #FFF;
	padding: 16px 6px 14px 14px;
	margin-right: 20px;
	border-radius: 8px;
	overflow-x: hidden;
	overflow-y: auto;
	max-height: calc(80vh - 70px);
}
#BAP-Wrapper::-webkit-scrollbar {
	background-color: #0000;
	width: 5px;
}
#BAP-Wrapper::-webkit-scrollbar-thumb {
	background-color: #CCC;
	border-radius: 99px;
}
#BAP-Wrapper::-webkit-scrollbar-thumb:hover {
	background-color: #BBB;
}
#BAP-Wrapper * {
	overflow: hidden;
	text-overflow: ellipsis;
}
/*列表*/
#BAP-List {
	list-style-position: inside;
	padding-left: 0;
	white-space: nowrap;
}
#BAP-List ul{
	padding-left: 1em;
}

/*更多样式*/
[BAP-theme=block] #BAP-Wrapper::-webkit-scrollbar-button:single-button {
	display: block;
	height: 5px;
	background-color: #0000;
}
[BAP-theme=note] #BAP-Wrapper {
	margin: 15px 0;
	border-radius: 8px 0 0 8px;
	padding-right: 0;
	border-right: 1px solid #DDE;
}
	`);


	$(document).ajaxComplete(function(e, xhr, opt) {
		if(opt.url.search(/WebApi\/Forum\/[0-9]*?\/GetPost$/) < 0) return;

		//自动展开
		$(".forum-post-view_unfold_1U9ge>span").click();

		//更改文档标题
		{
		let tt = document.title,
			dt = xhr.responseJSON.post.title;
		//超过10字则省略号
		document.title = dt.substr(0, 9) + (dt.length > 9 ? "…" : "")
			+ " - " + tt.substr(tt.lastIndexOf("论坛"));
		}

		//目录写入
		{
		const Headers = { //标签的缩进数量，大写
			H1: 0,
			H2: 1,
			H3: 2,
			H4: 3,
			H5: 4,
			H6: 5,
		};
		$(".container>.panel-large .panel2_panelContent_2esaW.panel-content")
			.prepend(`<div id="BAP-Wrapper"><ul id="BAP-List"></ul></div>`);
		let $List = $("#BAP-List");

		$(".forum-post-view_wrapper_s5oGn .markdown_body_1wo0f>.content *:not(:empty)")
			.filter(Object.keys(Headers).join(",")).each(function(i, h) { //遍历Headers里的标签
			var $head = $(h);
			let $item = $('<li></li>').html(
				$('<a></a>').text($head.text()).attr("title", $head.text())
				.on('click', function() {
					$("html, body").animate({
						scrollTop: $head.offset().top - 70
					}, 200);
				})
			);
			$List.append($item);
			let rpt = (str)=>(str.repeat(Headers[$head.prop("tagName")]));
			$item.wrap(rpt('<ul>') + rpt('</ul>'));
		});

		}
	});
})();
